﻿using gl_SMT.Model;
using Microsoft.Office.Interop.Excel;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Application = Microsoft.Office.Interop.Excel.Application;

namespace gl_SMT.View
{
    /// <summary>
    /// juki_DataSelect.xaml 的交互逻辑
    /// </summary>
    public partial class juki_DataSelect : UserControl
    {
        public juki_DataSelect()
        {
            InitializeComponent();
            this.Loaded += Juki_DataSelect_Loaded;
        }

        public string MachineName { get; set; }
        DatabaseReadWrite database = new DatabaseReadWrite();
        private void Juki_DataSelect_Loaded(object sender, RoutedEventArgs e)
        {
            dp_SatrtDate.Text = DateTime.Now.ToString();
            dp_EndDate.Text = DateTime.Now.ToString();
            SelectData();
        }

        private void btn_Select_Click(object sender, RoutedEventArgs e)
        {
            SelectData();
        }

        private void SelectData()
        {
            List<juki_model> juki_Model = database.QueryData<juki_model>("juki_Producedate", "MachineName", $"{MachineName.Split(' ')[1]} {MachineName.Split(' ')[2]}", dp_SatrtDate.Text, dp_EndDate.Text, "Time","date");
            dataGrid.ItemsSource = juki_Model;
            txt_SelectNumber.Text = juki_Model.Count.ToString();
        }

        private void btn_Export_Click(object sender, RoutedEventArgs e)
        {
            List<juki_model> juki_Model = database.QueryData<juki_model>("juki_Producedate", "MachineName", $"{MachineName.Split(' ')[1]} {MachineName.Split(' ')[2]}", dp_SatrtDate.Text, dp_EndDate.Text, "Time", "date");
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Filter = "Excel Worksheets|*.xls";
            if (saveFile.ShowDialog() == true)
            {
                Application excelApp = new Application();
                Workbook workbook = excelApp.Workbooks.Add();
                Worksheet worksheet = (Worksheet)workbook.ActiveSheet;
                for (int i = 0; i < dataGrid.Columns.Count; i++)
                {
                    worksheet.Cells[1, i + 1] = dataGrid.Columns[i].Header.ToString();// 设置表头
                }
                for (int i = 0; i < juki_Model.Count; i++)
                {
                    worksheet.Cells[2 + i, 1] = juki_Model[i].MachineName;
                    worksheet.Cells[2 + i, 2] = juki_Model[i].model;
                    worksheet.Cells[2 + i, 3] = juki_Model[i].StartTime;
                    worksheet.Cells[2 + i, 4] = juki_Model[i].StopTime;
                    worksheet.Cells[2 + i, 5] = juki_Model[i].RunningTime;
                    worksheet.Cells[2 + i, 6] = juki_Model[i].PwbWaitTime;
                    worksheet.Cells[2 + i, 7] = juki_Model[i].PwbInWaitTime;
                    worksheet.Cells[2 + i, 8] = juki_Model[i].PwbOutWaitTime;
                    worksheet.Cells[2 + i, 9] = juki_Model[i].PwbWaitTimeDual;
                    worksheet.Cells[2 + i, 10] = juki_Model[i].SomeStopTime;
                    worksheet.Cells[2 + i, 11] = juki_Model[i].RepairTime;
                    worksheet.Cells[2 + i, 12] = juki_Model[i].ToubleTime;
                    worksheet.Cells[2 + i, 13] = juki_Model[i].NoCompoStopTime;
                    worksheet.Cells[2 + i, 14] = juki_Model[i].ProductDownTime;
                    worksheet.Cells[2 + i, 15] = juki_Model[i].ProductNum;
                    worksheet.Cells[2 + i, 16] = juki_Model[i].ProductCircuit;
                    worksheet.Cells[2 + i, 17] = juki_Model[i].TotalPick;
                    worksheet.Cells[2 + i, 18] = juki_Model[i].TotalPlace;
                    worksheet.Cells[2 + i, 19] = juki_Model[i].Time;
                    worksheet.Cells[2 + i, 20] = juki_Model[i].jsonFileName;
                }
                // 自动调整列宽
                worksheet.Columns.AutoFit();

                // 保存文件
                workbook.SaveAs(saveFile.FileName);
                workbook.Close();
                excelApp.Quit();

                // 释放 COM 对象
                System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
                MessageBox.Show("导出完成");
            }
        }
    }
}
